package pl.rzarajczyk.breaktime.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.IOException;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import pl.rzarajczyk.breaktime.Lang;
import pl.rzarajczyk.breaktime.Localization;
import pl.rzarajczyk.breaktime.BreakTimeSettings;
import pl.rzarajczyk.breaktime.MonitorPowerUsageControllState;
import pl.rzarajczyk.utils.monitorcontroller.MonitorControllerFactory;
import pl.rzarajczyk.breaktime.utils.ExtendedJFrame;
import pl.rzarajczyk.breaktime.utils.Utils;

/**
 *
 * @author rzarajczyk
 */
public class BreakWindow extends ExtendedJFrame {
    @Autowired
    private BreakTimeSettings settings;

    @Autowired
    private Localization localization;
    
    private int length;
    private Log log = LogFactory.getLog(getClass());

    public BreakWindow() {
        initComponents();
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        if ( ! gd.isFullScreenSupported() ) {
            log.warn("Exclusive full screen is not supported by this system");
        }
    }
    
    @PostConstruct
    public void open() {
        this.getContentPane().setBackground(Color.BLACK);
    }

    public void start(int length) {
        if ( !settings.isShowCursor() ) {
            hideMouseCursor();
        } else {
            setCursor(null);
        }
        this.progress.setMaximum(length);
        this.progressLabel.setText("00:00");
        this.progress.setValue(0);
        this.length = length;
        this.setControllsVisible( settings.isShowControlls() );
        this.setFullScreen(true);
        this.setVisible(true);
        if ( shouldManageMonitorPowerUsage() ) {
            try {
                MonitorControllerFactory.getMonitorController().turnOff();
            } catch (IOException e) {
                Utils.error(this, log, "Cannot turn off the monitor", e);
            }
        }
        log.debug("BreakWindow started");
    }

    public void tick(int current) {
        this.progress.setValue(length - current);
        this.progressLabel.setText( Utils.toTimeString(current) );
    }

    public void stop() {
        if ( shouldManageMonitorPowerUsage() ) {
            try {
                MonitorControllerFactory.getMonitorController().turnOn();
            } catch (IOException e) {
                Utils.error(this, log, "Cannot turn on the monitor", e);
            }
        }
        this.setVisible(false);
        this.setFullScreen(false);
        log.debug("BreakWindow hidden");
    }

    public void setControllsVisible(boolean show) {
        this.controllsPanel.setVisible(show);
    }

    public boolean isControllsVisible() {
        return this.controllsPanel.isVisible();
    }
    
    @Override
    public void setVisible(boolean visible) {
        if ( visible ) {
            localization.localize(this);
        }
        
        super.setVisible(visible);
    }
    
    private void hideMouseCursor() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.createImage("blank.gif");
        Point hotSpot = new Point(0,0);  
        Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Blank");  
        setCursor(cursor);
    }
    
    private boolean shouldManageMonitorPowerUsage() {
        MonitorPowerUsageControllState state = settings.getMonitorPowerUsageControll();
        switch ( state ) {
            case DISABLED:
                return false;
            case ENABLED:
                return true;
            case AUTO:
                return !settings.isShowControlls() && !settings.isShowCursor();
            default:
                log.warn("Unknown enum value: " + state);
                return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controllsPanel = new javax.swing.JPanel();
        progress = new javax.swing.JProgressBar();
        progressLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Break time!");
        setUndecorated(true);

        controllsPanel.setBackground(new java.awt.Color(0, 0, 0));

        progressLabel.setForeground(new java.awt.Color(255, 255, 255));
        progressLabel.setText("jLabel2");

        titleLabel.setFont(new java.awt.Font("Dialog", 1, 24));
        titleLabel.setForeground(new java.awt.Color(255, 255, 255));
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        titleLabel.setText("Break time!");

        javax.swing.GroupLayout controllsPanelLayout = new javax.swing.GroupLayout(controllsPanel);
        controllsPanel.setLayout(controllsPanelLayout);
        controllsPanelLayout.setHorizontalGroup(
            controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(controllsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controllsPanelLayout.createSequentialGroup()
                        .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progressLabel))
                    .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE))
                .addContainerGap())
        );
        controllsPanelLayout.setVerticalGroup(
            controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controllsPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(controllsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(progressLabel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(controllsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(133, Short.MAX_VALUE)
                .addComponent(controllsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel controllsPanel;
    private javax.swing.JProgressBar progress;
    private javax.swing.JLabel progressLabel;
    @Lang
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables


}
